<?php
/**
 * @title 894. 所有可能的真二叉树
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    public $dp;

    /**
     * @param Integer $n
     * @return TreeNode[]
     */
    function allPossibleFBT($n) {
        /**
         * @since 2024-04-19 命中缓存
         */
        $k = $n;
        if(isset($this->dp[$k])){
            return $this->dp[$k];
        } else {}

        /**
         * @since 2024-04-23 偶数无解
         */
        if($n%2 == 0) return [];

        /**
         * @since 2024-04-19 每棵树的每个节点都必须符合 Node.val == 0
         * @since 2024-04-23 只有一个节点
         */
        if($n == 1) return [new TreeNode(0)];
        $n--;

        /**
         * @since 2024-04-23 左子树节点数量，1,3,5,...
         */
        $nodes = [];
        for($i=1; $i<$n; $i+=2){
            $leftNodeArray = self::allPossibleFBT($i);
            foreach ($leftNodeArray as $leftNode){
                $rightNodeArray = self::allPossibleFBT($n-$i);
                foreach ($rightNodeArray as $rightNode){
                    $nodes[] = new TreeNode(0, $leftNode, $rightNode);
                }
            }
        }

        /**
         * @return
         */
        return $this->dp[$k] = $nodes;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202404/2024.04.02.php
 */
$datas = [
    [7],
    [5],
    [3],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->allPossibleFBT(...$data);
    // echo $xhprof->DumpArgs([$result]);
    foreach ($result as $r){
        $arr = $xhprof->treeArray($r);
        echo "[", implode(",", $arr) ,"]", PHP_EOL;
    }
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
